﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helena.Areas.Productos.Models;
using Telerik.Web.Mvc;
using Helena.Areas.Entradas.Models;
using Helena.Areas.Vendedores.Models;
using Helena.Areas.Proveedores.Models;
using Helena.Areas.Reportes.Models;

namespace Helena.Areas.Entradas.Controllers
{
    public class EntradaController : Controller
    {
        LogicaEntrada db = new LogicaEntrada();
        LogicaProveedor pro_db = new LogicaProveedor();
        

        Vendedor vd = null;

        #region Comun

        public ActionResult Index()
        {
            VendedorView vw = (VendedorView)Session["Vendedor"];
            try
            {
                if (vw.ID != 0)
                {
                    if (vw.admin)
                    {
                        ObtenerProveedores();
                        ObtenerVendedores();
                        return View();
                    }
                    else
                    {
                        return Redirect("../Home/Error");
                    }
                }
                else
                {
                    return Redirect("../Home/NoSession");
                }
            }
            catch
            {
                return Redirect("../Home/NoSession");
            }

        }

        [GridAction]
        public ActionResult Select()
        {
            return View("Index", new GridModel(db.i_RetornarEntradas()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Insert()
        {
            EntradaView e_v = new EntradaView();
            if (TryUpdateModel(e_v))
            {
                e_v.VendedorID=((VendedorView)Session["Vendedor"]).ID;
                db.i_AgregarEntrada(e_v);
            }
            return View("Index", new GridModel(db.i_RetornarEntradas()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(int? id)
        {
            int ID = id ?? -1;
            db.i_EliminarEntrada(ID);
            return View("Index", new GridModel(db.i_RetornarEntradas()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Update(EntradaView e_v)
        {
            e_v.VendedorID=((VendedorView)Session["Vendedor"]).ID;
            db.i_ModificarEntrada(e_v);
            return View("Index", new GridModel(db.i_RetornarEntradas()));
        }

        #endregion

        #region Detalles

        public ActionResult AddDetalles(int EntradaID)
        {
            Session["EntradaSeleccionada"] = EntradaID;
            return Redirect("../../DetalleEntrada");
        }

        #endregion

        #region Extra

        private void ObtenerProveedores()
        {
            ViewData["Proveedores"] = pro_db.i_RetornarProveedores();
        }

        private void ObtenerVendedores()
        {
            List<VendedorView> salida = new List<VendedorView>();
            salida.Add((VendedorView)Session["Vendedor"]);
            ViewData["Vendedor"] = salida;
        }
        #endregion
    }
}
